import Vue from "vue";
import VueRouter from "vue-router";
import { RoutesTool } from "@/utils/frame/RouteTool";
import { ViewItems } from "@/model/frame/ViewItems";
import layoutConfig from "./LayoutConfig";

// 导入自定义配置
import customGrad from "@/frame-config/router/grad";
import customRoute from "@/frame-config/router/route";

Vue.use(VueRouter);

//region 路由生成
// 读取 ViewItems 配置项内容
const viewRoutes = RoutesTool.layout(
  RoutesTool.viewItemRoutes(ViewItems.instance().groups)
);

// 这里是一些默认页面的配置
// 如果觉得页面不好，可以引用自己的页面或者修改默认的界面
const notFound = RoutesTool.notFound("frame/NotFound.vue");
const comingSoon = RoutesTool.comingSoon("frame/ComingSoon.vue");
const maintenance = RoutesTool.maintenance("frame/Maintenance.vue");
const noPermission = RoutesTool.noPermission("frame/NoPermission.vue");
//endregion

// region 路由注册[不可修改]
const router = new VueRouter({
  // mode: "history",
  mode: process.env.NODE_ENV === "production" ? "history" : "hash",
  base: process.env.BASE_URL,
  scrollBehavior: (to, from, savedPosition) => {
    // 让每次的导航回到相同的位置
    // 现在不知道具体用处
    if (to.hash) {
      return { selector: to.hash };
    }

    if (savedPosition) {
      return savedPosition;
    }

    return { x: 0, y: 0 };
  },
  // 将生成的路由配置,填写入路由属性中
  routes: [viewRoutes, notFound, comingSoon, maintenance, noPermission]
});

// 追加的路由信息注册
customRoute.forEach(item => {
  router.addRoute(item);
});
layoutConfig.layoutRoute.forEach(item => {
  router.addRoute(item);
});

// 导入路由的守护配置
router.beforeEach(layoutConfig.beforeGrad);
customGrad.beforeEach.forEach(item => {
  router.beforeEach(item);
});

customGrad.beforeResolve.forEach(item => {
  router.beforeResolve(item);
});

export default router;
// endregion
